package com.tzbank.common.service.impl;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tzbank.common.dao.FrequentContactsMapper;
import com.tzbank.common.service.FrequentContactsService;
import com.tzbank.entity.FrequentContacts;
import com.tzbank.utils.CodeUtil;
import com.tzbank.utils.ResultUtil;

@Service
public class FrequentContactsServiceImpl implements FrequentContactsService {

	@Autowired
	private FrequentContactsMapper frequentContactsMapper;

	@Transactional
	@Override
	public Map<String, Object> addContacts(FrequentContacts contacts) {
		contacts.setCreateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		contacts.setUpdateDate(contacts.getCreateDate());
		this.frequentContactsMapper.insert(contacts);
		return ResultUtil.resultMap(CodeUtil.CODE_200.getName(), CodeUtil.CODE_200.getCode(), null);
	}

	@Transactional
	@Override
	public Map<String, Object> modifyContacts(FrequentContacts contacts) {
		contacts.setUpdateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		this.frequentContactsMapper.update(contacts);
		return ResultUtil.resultMap(CodeUtil.CODE_200.getName(), CodeUtil.CODE_200.getCode(), null);
	}

	@Transactional
	@Override
	public Map<String, Object> deleteContacts(FrequentContacts contacts) {
		this.frequentContactsMapper.deleteByAutoId(contacts.getAutoId());
		// 重新排序
		List<FrequentContacts> contactsItems = this.frequentContactsMapper.selectByUserId(contacts.getUserId());
		Iterator<FrequentContacts> iterator = contactsItems.iterator();
		while (iterator.hasNext()) {
			if (iterator.next().getAutoId() == contacts.getAutoId())
				contactsItems.remove(iterator.next());
		}
		Collections.sort(contactsItems, new Comparator<FrequentContacts>() {

			@Override
			public int compare(FrequentContacts f1, FrequentContacts f2) {
				return f1.getSort() - f2.getSort();
			}

		});
		this.frequentContactsMapper.updateList(contactsItems);
		return ResultUtil.resultMap(CodeUtil.CODE_200.getName(), CodeUtil.CODE_200.getCode(), null);
	}

	@Override
	public Map<String, Object> queryContactsByUserId(String userId) {
		List<FrequentContacts> contactsItems = this.frequentContactsMapper.selectByUserId(userId);
		return ResultUtil.resultMap(CodeUtil.CODE_200.getName(), CodeUtil.CODE_200.getCode(), contactsItems);
	}

	@Override
	public Map<String, Object> queryContactsByUserIdAndName(String userId, String name) {
		FrequentContacts contacts = this.frequentContactsMapper.selectByUserIdAndName(userId, name);
		return ResultUtil.resultMap(CodeUtil.CODE_200.getName(), CodeUtil.CODE_200.getCode(), contacts);
	}

}
